package com.dz.risk.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dz.risk.common.ResponseVO;
import com.dz.risk.common.exception.RRException;
import com.dz.risk.pojo.QueryParams;
import com.dz.risk.pojo.domain.BasicBondInformationEntity;
import com.dz.risk.pojo.domain.BondRiskInformationSheetEntity;
import com.dz.risk.pojo.domain.EarlyWarningDebtListEntity;
import com.dz.risk.pojo.domain.IndicatorDetailsEntity;
import com.dz.risk.pojo.model.PageModel;
import com.dz.risk.service.IndicatorDetailsService;
import com.dz.risk.validation.Add;
import com.dz.risk.validation.Update;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


/**
 * 指标项详情
 *
 * @author yinpl
 * @email 
 * @date 2021-08-09 09:21:03
 */
@RestController
@Api(tags = "指标项详情api", value = "指标项详情接口")
@RequestMapping("/dz-ftsp/stest/indicatordetails")
public class IndicatorDetailsController {
    @Autowired
    private IndicatorDetailsService indicatorDetailsService;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    /**
     * 查询指标项详情
     *
     */
    @ApiOperation("查询指标项详情")
    @GetMapping(value = "/query")
    public ResponseVO query( PageModel pageModel,IndicatorDetailsEntity queryParams) {
        IPage<IndicatorDetailsEntity> iPage =null;
        try {

            iPage = indicatorDetailsService.query(pageModel,queryParams);
        } catch (RRException e) {
            e.printStackTrace();
            logger.error("查询日志信息失败！" + e);
            return new ResponseVO(e.getCode(), e.getMessage());
        }
        return ResponseVO.ok(iPage);

    }

    /**
     * 获取指标项详情
     *
     */
    @ApiOperation("获取指标项详情")
    @GetMapping(value = "/queryById")
    public ResponseVO queryById( String indexNumber) {
        IndicatorDetailsEntity indicatorDetailsEntity =null;
        try {
            if (indexNumber != null) {
                indicatorDetailsEntity = indicatorDetailsService.get(indexNumber);
            }
        } catch (RRException e) {
            e.printStackTrace();
            logger.error("查询日志信息失败！" + e);
            return new ResponseVO(e.getCode(), e.getMessage());
        }
        return ResponseVO.ok(indicatorDetailsEntity);

    }

    @ApiOperation("新增指标项详情")
    @PostMapping("/add")
    public ResponseVO add(@RequestBody @Validated(Add.class) IndicatorDetailsEntity indicatorDetailsEntity, HttpServletRequest request) {
        indicatorDetailsEntity.setCreatedUser(request.getRemoteUser());
        indicatorDetailsEntity.setId(UUID.randomUUID().toString().replaceAll("-",""));
        return  indicatorDetailsService.add(indicatorDetailsEntity);
    }

    @ApiOperation("更新指标项详情")
    @PostMapping("/update")
    public ResponseVO updateById(@RequestBody @Validated(Update.class) IndicatorDetailsEntity indicatorDetailsEntity, HttpServletRequest request) {
        indicatorDetailsEntity.setUpdatedUser(request.getRemoteUser());
        indicatorDetailsEntity.setUpdatedTime(new Date());
        indicatorDetailsService.updateById(indicatorDetailsEntity);
        return ResponseVO.ok();
    }

    @ApiOperation("根据Id删除指标项详情")
    @GetMapping("/deleteById")
    public ResponseVO deleteById(String indexNumber) {
        indicatorDetailsService.deleteById(indexNumber);
        return ResponseVO.ok();
    }

    /**
     * 导入指标项详情
     *
     */
    @ApiOperation("导入指标项详情")
    @PostMapping("/upload")
    public ResponseVO upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        // params.setTitleRows(0);
        List<IndicatorDetailsEntity> result = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                IndicatorDetailsEntity.class, params);
        //判断是否有同编号同名的
        List<String> indexIds =  indicatorDetailsService.selectIndexIds();
        result = result.stream().filter(a -> !indexIds.contains(a.getIndexNumber())).collect(Collectors.toList());

        for (int i = 0; i < result.size(); i++) {
            IndicatorDetailsEntity detailsEntity = result.get(i);
            detailsEntity.setCreatedUser(request.getRemoteUser());
            detailsEntity.setCreatedTime(new Date());
            detailsEntity.setId(UUID.randomUUID().toString().replaceAll("-",""));
        }
        //添加集合
        indicatorDetailsService.saveBatch(result);

        return ResponseVO.ok("导入成功");
    }

}
